// 导入依赖
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

class JKLocalStorage {
  JKLocalStorage._internal();
  factory JKLocalStorage() => _instance;
  static final JKLocalStorage _instance = JKLocalStorage._internal();

  /// 根据key存储int类型
  static Future<bool> setInt(String key, int value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setInt(key, value);
  }

  /// 根据key获取int类型
  static Future<int?> getInt(String key, {int defaultValue = 0}) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getInt(key) ?? defaultValue;
  }

  /// 根据key存储double类型
  static Future<bool> setDouble(String key, double value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setDouble(key, value);
  }

  /// 根据key获取double类型
  static Future<double?> getDouble(String key,
      {double defaultValue = 0.0}) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getDouble(key) ?? defaultValue;
  }

  /// 根据key存储字符串类型
  static Future<bool> setString(String key, String value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setString(key, value);
  }

  /// 根据key获取字符串类型
  static Future<String?> getString(String key,
      {String defaultValue = ""}) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getString(key) ?? defaultValue;
  }

  /// 根据key存储布尔类型
  static Future<bool> setBool(String key, bool value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setBool(key, value);
  }

  /// 根据key获取布尔类型
  static Future<bool?> getBool(String key, {bool defaultValue = false}) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getBool(key) ?? defaultValue;
  }

  /// 根据key存储字符串类型数组
  static Future<bool> setStringList(String key, List<String> value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setStringList(key, value);
  }

  /// 根据key获取字符串类型数组
  static Future<List<String>> getStringList(String key,
      {List<String> defaultValue = const []}) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getStringList(key) ?? defaultValue;
  }

  /// 根据key存储Map类型
  static Future<bool> setMap(String key, Map value) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.setString(key, json.encode(value));
  }

  /// 根据key获取Map类型
  static Future<Map> getMap(String key) async {
    final preferences = await SharedPreferences.getInstance();
    String jsonStr = preferences.getString(key) ?? "";
    return jsonStr.isEmpty ? Map : json.decode(jsonStr);
  }

  /// 获取持久化数据中所有存入的key
  static Future<Set<String>> getKeys() async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.getKeys();
  }

  /// 获取持久化数据中是否包含某个key
  static Future<bool> containsKey(String key) async {
    final preferences = await SharedPreferences.getInstance();
    return preferences.containsKey(key);
  }

  /// 删除持久化数据中某个key
  static Future<bool> remove(String key) async {
    final preferences = await SharedPreferences.getInstance();
    return await preferences.remove(key);
  }

  /// 清除所有持久化数据
  static Future<bool> clear() async {
    final preferences = await SharedPreferences.getInstance();
    return await preferences.clear();
  }

  /// 重新加载所有数据,仅重载运行时
  static Future<void> reload() async {
    final preferences = await SharedPreferences.getInstance();
    return await preferences.reload();
  }

  /// 判断是否是json字符串
  static _isJson(String value) {
    try {
      JsonDecoder().convert(value);
      return true;
    } catch (e) {
      return false;
    }
  }
}
